package com.mamesoft.medicipazienti.servlet;

import com.mamesoft.medicipazienti.tools.JDBC;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class VisualizzaStoricoMedicoServlet extends HttpServlet {

    private static Logger log = Logger.getLogger(VisualizzaStoricoMedicoServlet.class);

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PropertyConfigurator.configure(VisualizzaStoricoMedicoServlet.class.getClassLoader().getResource("com/mamesoft/medicipazienti/tools/log4j.properties"));
        Connection connect = null;
        PrintWriter out = null;
        try {
            HttpSession session = request.getSession();
            String nome = (String) session.getAttribute("nome");
            String cognome = (String) session.getAttribute("cognome");
            String anno = request.getParameter("anno");
            String mese = request.getParameter("mese");
            String giorno = request.getParameter("giorno");
            String ora = request.getParameter("ora");
            String minuti = request.getParameter("minuti");
            String secondi = request.getParameter("secondi");
            Boolean tutti = false;
            if (request.getParameter("medico") != null) {
                tutti = (request.getParameter("medico").equalsIgnoreCase("Tutti"));
            }

            ResultSet res = null;
            Timestamp tS = null;
            DateFormat c = null;

            Boolean ok = false;
            String query = "";

            if (anno != null & mese != null & giorno != null & ora != null & minuti != null & secondi != null) {
                ok = true;
                tS = Timestamp.valueOf(anno + "-" + mese + "-" + giorno + " " + ora + ":" + minuti + ":" + secondi);
                c = DateFormat.getDateTimeInstance();

                connect = JDBC.connect();

                query = "SELECT vaccino.data AS Data, medico.nome AS NomeMedico, medico.cognome AS CognomeMedico, "
                        + "paziente.nome AS NomePaziene, paziente.cognome AS CognomePaziente,paziente.ID AS pID\n"
                        + "FROM vaccino JOIN medico ON ( vaccino.mID = medico.ID )\n"
                        + "JOIN paziente ON ( vaccino.pID = paziente.ID )\n"
                        + "WHERE vaccino.data BETWEEN TIMESTAMP '" + tS.toString() + "' AND CURRENT_TIMESTAMP\n";
                if (!tutti) {
                    query += "AND medico.nome='" + nome + "' AND medico.cognome='" + cognome + "'\n";
                }
                query += "ORDER BY Data DESC;";
                res = JDBC.executeQuery(query, connect);
            }

            response.setContentType("text/html;charset=UTF-8");
            out = response.getWriter();

            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("    <head>");
            out.println("        <title>Vaccino Manger</title>");
            out.println("        <link rel=\"stylesheet\" type=\"text/css\" href=\"/MediciPazienti/variant-stream.css\" title=\"Variant Stream\" media=\"screen,projection\" />");
            out.println("        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">       ");
            out.println("    </head>");

            out.println("    <body>");
            out.println("        <div id =\"wrap\">");
            out.println("            <div id=\"header\">");
            out.println("                <div id =\"topmenu\">");
            out.println("                    <p>");
            out.println("                        <a href=\"/MediciPazienti/logout\">Logout</a>");
            out.println("                    </p>");
            out.println("                </div>");
            out.println("                <div id=\"title\">");
            out.println("                    <h1>Vaccino Manager</h1><br/>");
            out.println("                    <h2 class =\"slogan\">I tuoi vaccini contano</h2>");
            out.println("                </div>");
            out.println("            </div>");

            out.println("            <div id=\"sidebar\">");
            out.println("               <ul class = \"sidemenu\">");
            out.println("                   <li><a href=\"/MediciPazienti/medico/welcome\">Home</a><li>");
            out.println("                   <li><a href=\"/MediciPazienti/medico/richiama\">Invia richiami</a><li>");
            out.println("                   <li><a class=\"active\" href=\"/MediciPazienti/medico/visualizza_storico\">Visualizza Storico</a><li>");
            out.println("                   <li><a href=\"/MediciPazienti/logout\">Logout </a><li>");
            out.println("               </ul>");
            out.println("            </div>");

            out.println("            <div id =\"content\">");
            out.println("                <div id=\"feature\">");
            out.println("                    <p>" + session.getAttribute("last_login") + "</p>");
            out.println("</p>");
            out.println("                </div>");



            if (ok) {

                out.println("                <div class=\"post\">");
                out.println("                    <h2>Riepilogo vaccini dal " + c.format(tS) + "</h2>");
                //out.println(query+"<br>");
                out.println("                    <table border=\"0\">");
                out.println("                        <tbody>");
                out.println("                            <tr>");
                out.println("                                <td>");

                if (res.next()) {
                    out.println("<table border=\"1\" width=\"120%\" cellspacing=\"0\">");
                    out.println("    <thead>");
                    out.println("        <tr>");
                    out.println("            <th>DATA VACCINAZIONE</th>");
                    out.println("            <th>MEDICO</th>");
                    out.println("            <th>PAZIENTE</th>");
                    out.println("        </tr>");
                    out.println("    </thead>");
                    do {
                        Date time = res.getTimestamp("Data");
                        String nomeMedico = res.getString("NomeMedico");
                        String cognomeMedico = res.getString("CognomeMedico");
                        String nomePaziente = res.getString("NomePaziene");
                        String cognomePaziente = res.getString("CognomePaziente");
                        int pID = res.getInt("pID");

                        nomeMedico = nomeMedico.substring(0, 1).toUpperCase() + nomeMedico.substring(1).toLowerCase();
                        cognomeMedico = cognomeMedico.substring(0, 1).toUpperCase() + cognomeMedico.substring(1).toLowerCase();
                        nomePaziente = nomePaziente.substring(0, 1).toUpperCase() + nomePaziente.substring(1).toLowerCase();
                        cognomePaziente = cognomePaziente.substring(0, 1).toUpperCase() + cognomePaziente.substring(1).toLowerCase();
                        out.println("    <tr>");
                        out.println("        <td> " + c.format(time) + "</td>");
                        out.println("        <td> Dr. " + nomeMedico + " " + cognomeMedico + " </td>");
                        out.println("        <td> <a href=/MediciPazienti/medico/dettaglio_paziente?id=" + pID + ">" + nomePaziente + " " + cognomePaziente + "</a> </td>");
                        out.println("    </tr>");
                    } while (res.next());
                    out.println("</tbody>");
                    out.println("</table>");
                    res.close();
                } else {
                    out.println("Non ci sono state vaccinazioni dal " + c.format(tS) + " a oggi");
                }

            } else {

                out.println("                <div class=\"post\">");
                out.println("                    <h2>Riepilogo vaccini</h2>");
                out.println("                    <p>Selezionare data e ora da cui si desidera avere i dati:</p>");
                out.println("                    <table border=\"0\">");
                out.println("                        <tbody>");
                out.println("                            <tr>");
                out.println("                                <td>");
                out.println("<form name=\"data\" action=\"visualizza_storico\" method=\"POST\">");
                out.println("    <b>Data:</b><br>");
                out.println("    Giorno");
                out.println("    <select name=\"giorno\">");
                out.println("        <option>01</option>");
                out.println("        <option>02</option>");
                out.println("        <option>03</option>");
                out.println("        <option>04</option>");
                out.println("        <option>05</option>");
                out.println("        <option>06</option>");
                out.println("        <option>07</option>");
                out.println("        <option>08</option>");
                out.println("        <option>09</option>");
                out.println("        <option>10</option>");
                out.println("        <option>11</option>");
                out.println("        <option>12</option>");
                out.println("        <option>13</option>");
                out.println("        <option>14</option>");
                out.println("        <option>15</option>");
                out.println("        <option>16</option>");
                out.println("        <option>17</option>");
                out.println("        <option>18</option>");
                out.println("        <option>19</option>");
                out.println("        <option>20</option>");
                out.println("        <option>21</option>");
                out.println("        <option>22</option>");
                out.println("        <option>23</option>");
                out.println("        <option>24</option>");
                out.println("        <option>25</option>");
                out.println("        <option>26</option>");
                out.println("        <option>27</option>");
                out.println("        <option>28</option>");
                out.println("        <option>29</option>");
                out.println("        <option>30</option>");
                out.println("        <option>31</option>");
                out.println("    </select>");
                out.println("    Mese");
                out.println("    <select name=\"mese\">");
                out.println("        <option>01</option>");
                out.println("        <option>02</option>");
                out.println("        <option>03</option>");
                out.println("        <option>04</option>");
                out.println("        <option>05</option>");
                out.println("        <option>06</option>");
                out.println("        <option>07</option>");
                out.println("        <option>08</option>");
                out.println("        <option>09</option>");
                out.println("        <option>10</option>");
                out.println("        <option>11</option>");
                out.println("        <option>12</option>");
                out.println("    </select>");
                out.println("    Anno");
                out.println("    <select name=\"anno\">");
                out.println("        <option>2000</option>");
                out.println("        <option>2001</option>");
                out.println("        <option>2002</option>");
                out.println("        <option>2003</option>");
                out.println("        <option>2004</option>");
                out.println("        <option>2005</option>");
                out.println("        <option>2006</option>");
                out.println("        <option>2007</option>");
                out.println("        <option>2008</option>");
                out.println("        <option>2009</option>");
                out.println("        <option>2010</option>");
                out.println("        <option>2011</option>");
                out.println("    </select>");
                out.println("    <br><br><b>Ora:</b><br>");
                out.println("    Ore");
                out.println("    <select name=\"ora\">");
                out.println("        <option>00</option>");
                out.println("        <option>01</option>");
                out.println("        <option>02</option>");
                out.println("        <option>03</option>");
                out.println("        <option>04</option>");
                out.println("        <option>05</option>");
                out.println("        <option>06</option>");
                out.println("        <option>07</option>");
                out.println("        <option>08</option>");
                out.println("        <option>09</option>");
                out.println("        <option>10</option>");
                out.println("        <option>11</option>");
                out.println("        <option>12</option>");
                out.println("        <option>13</option>");
                out.println("        <option>14</option>");
                out.println("        <option>15</option>");
                out.println("        <option>16</option>");
                out.println("        <option>17</option>");
                out.println("        <option>18</option>");
                out.println("        <option>19</option>");
                out.println("        <option>20</option>");
                out.println("        <option>21</option>");
                out.println("        <option>22</option>");
                out.println("        <option>23</option>");
                out.println("        <option>24</option>");
                out.println("    </select>");
                out.println("    Minuti");
                out.println("    <select name=\"minuti\">");
                out.println("        <option>00</option>");
                out.println("        <option>01</option>");
                out.println("        <option>02</option>");
                out.println("        <option>03</option>");
                out.println("        <option>04</option>");
                out.println("        <option>05</option>");
                out.println("        <option>06</option>");
                out.println("        <option>07</option>");
                out.println("        <option>08</option>");
                out.println("        <option>09</option>");
                out.println("        <option>10</option>");
                out.println("        <option>11</option>");
                out.println("        <option>12</option>");
                out.println("        <option>13</option>");
                out.println("        <option>14</option>");
                out.println("        <option>15</option>");
                out.println("        <option>16</option>");
                out.println("        <option>17</option>");
                out.println("        <option>18</option>");
                out.println("        <option>19</option>");
                out.println("        <option>20</option>");
                out.println("        <option>21</option>");
                out.println("        <option>22</option>");
                out.println("        <option>23</option>");
                out.println("        <option>24</option>");
                out.println("        <option>25</option>");
                out.println("        <option>26</option>");
                out.println("        <option>27</option>");
                out.println("        <option>28</option>");
                out.println("        <option>29</option>");
                out.println("        <option>30</option>");
                out.println("        <option>31</option>");
                out.println("        <option>32</option>");
                out.println("        <option>33</option>");
                out.println("        <option>34</option>");
                out.println("        <option>35</option>");
                out.println("        <option>36</option>");
                out.println("        <option>37</option>");
                out.println("        <option>38</option>");
                out.println("        <option>39</option>");
                out.println("        <option>40</option>");
                out.println("        <option>41</option>");
                out.println("        <option>42</option>");
                out.println("        <option>43</option>");
                out.println("        <option>44</option>");
                out.println("        <option>45</option>");
                out.println("        <option>46</option>");
                out.println("        <option>47</option>");
                out.println("        <option>48</option>");
                out.println("        <option>49</option>");
                out.println("        <option>50</option>");
                out.println("        <option>51</option>");
                out.println("        <option>52</option>");
                out.println("        <option>53</option>");
                out.println("        <option>54</option>");
                out.println("        <option>55</option>");
                out.println("        <option>56</option>");
                out.println("        <option>57</option>");
                out.println("        <option>58</option>");
                out.println("        <option>59</option>");
                out.println("    </select>");
                out.println("    Secondi");
                out.println("    <select name=\"secondi\">");
                out.println("        <option>00</option>");
                out.println("        <option>01</option>");
                out.println("        <option>02</option>");
                out.println("        <option>03</option>");
                out.println("        <option>04</option>");
                out.println("        <option>05</option>");
                out.println("        <option>06</option>");
                out.println("        <option>07</option>");
                out.println("        <option>08</option>");
                out.println("        <option>09</option>");
                out.println("        <option>10</option>");
                out.println("        <option>11</option>");
                out.println("        <option>12</option>");
                out.println("        <option>13</option>");
                out.println("        <option>14</option>");
                out.println("        <option>15</option>");
                out.println("        <option>16</option>");
                out.println("        <option>17</option>");
                out.println("        <option>18</option>");
                out.println("        <option>19</option>");
                out.println("        <option>20</option>");
                out.println("        <option>21</option>");
                out.println("        <option>22</option>");
                out.println("        <option>23</option>");
                out.println("        <option>24</option>");
                out.println("        <option>25</option>");
                out.println("        <option>26</option>");
                out.println("        <option>27</option>");
                out.println("        <option>28</option>");
                out.println("        <option>29</option>");
                out.println("        <option>30</option>");
                out.println("        <option>31</option>");
                out.println("        <option>32</option>");
                out.println("        <option>33</option>");
                out.println("        <option>34</option>");
                out.println("        <option>35</option>");
                out.println("        <option>36</option>");
                out.println("        <option>37</option>");
                out.println("        <option>38</option>");
                out.println("        <option>39</option>");
                out.println("        <option>40</option>");
                out.println("        <option>41</option>");
                out.println("        <option>42</option>");
                out.println("        <option>43</option>");
                out.println("        <option>44</option>");
                out.println("        <option>45</option>");
                out.println("        <option>46</option>");
                out.println("        <option>47</option>");
                out.println("        <option>48</option>");
                out.println("        <option>49</option>");
                out.println("        <option>50</option>");
                out.println("        <option>51</option>");
                out.println("        <option>52</option>");
                out.println("        <option>53</option>");
                out.println("        <option>54</option>");
                out.println("        <option>55</option>");
                out.println("        <option>56</option>");
                out.println("        <option>57</option>");
                out.println("        <option>58</option>");
                out.println("        <option>59</option>");
                out.println("    </select>");
                out.println("    <br><br><b>Medico:</b><br>");
                out.println("    <select name=\"medico\">");
                out.println("        <option>Solo io</option>");
                out.println("        <option>Tutti</option>");
                out.println("    </select>");
                out.println("    <br><br><input type=\"submit\" value=\"Procedi\" />");
                out.println("</form>");
            }

            out.println("                            </tr>");
            out.println("                        </tbody>");
            out.println("                    </table>");
            out.println("                </div>");
            out.println("            </div>");
            out.println("            <div id=\"footer\">");
            out.println("                <p class=\"centered\">Copyright &copy; 2011 <a href=\"/MediciPazienti/index.html\">Mamesoft</a>");
            out.println("                    Progetto Programmazione per il web A.A.2011/2012</p>");
            out.println("            </div>");
            out.println("        </div>");
            out.println("    </body>");
            out.println("</html>");

        } catch (Exception ex) {
            request.getSession().invalidate();
            if (ex instanceof SQLException | (ex instanceof ClassNotFoundException & ex instanceof InstantiationException & ex instanceof IllegalAccessException)) {
                log.error("Database Error: " + ex.toString());
                response.sendRedirect("/MediciPazienti/database_error.html");
            } else {
                log.error("Generic error: " + ex.toString());
                response.sendRedirect("/MediciPazienti/general_error.html");
            }
        } finally {
            if (out != null) {
                out.close();
            }
            if (connect != null) {
                try {
                    connect.close();
                } catch (Exception ex) {
                    request.getSession().invalidate();
                    log.error("Database Error: " + ex.toString());
                    response.sendRedirect("/MediciPazienti/database_error.html");
                }
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
